import sys
from gallery_class import Gallery

def _create_dataset ( start_point, lines, rows, tot_rows ):
	res = {}

	count = 0
	for v in rows:
		v [ "_pos" ] = start_point + count
		res [ "row%d" % count ] = v
		count += 1

	res [ 'rows' ] = tot_rows
	res [ 'from_row' ] = start_point
	res [ 'to_row' ] = start_point + count
	res [ 'lines' ] = lines

	return res

def get_gallery ( liwe, iid = 0, name = '' ):
	"""
	:INPUT:
		- iid:	SAFE_NUMBER
		- name:	SAFE_DB_STRING
	"""
	ga = Gallery ( liwe )
	res = ga.get_gallery ( iid = iid, name = name )
	return { 'gallery' : res }


def list_galleries ( liwe,  quant = 10, _X_LINES= -1, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:
		- quant:		SAFE_NUMBER
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT: 	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER
	"""
	use_dataset = 0 
                                
        if _X_LINES != -1:                                                                                            
                quant = _X_LINES
                use_dataset = 1 
                        
        if _X_START_POINT:                                                                                            
                start_point = _X_START_POINT
        else:                                                                                                         
                start_point = _X_PAGE * quant 
	
	ga = Gallery ( liwe )
        res = ga.list_galleries ( quant = quant, start_point = start_point, do_count = use_dataset )

	rows = res [ 'rows' ]
	tot_rows = res [ 'tot_rows' ]

        if not use_dataset: 
                if not rows: rows = []
                return { 'rows': rows }

        return _create_dataset ( start_point, quant, rows, tot_rows )



def list_imgs ( liwe, id_gal = 0, gal_name = '', kind = '', ext = '', tag = '', _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:
		- id_gal:		SAFE_NUMBER
		- gal_name:		SAFE_DB_STRING
		- kind:			SAFE_DB_STRING
		- ext:			SAFE_DB_STRING
		- tag:			SAFE_DB_STRING
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER
	"""	
	use_dataset = 0
	quant = 1

	if _X_LINES != -1: 
		quant = _X_LINES
		use_dataset = 1

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * quant


        ga = Gallery ( liwe )
	res = ga.list_imgs ( id_gal = id_gal, gal_name = gal_name, kind = kind, ext = ext , tag = tag , start_point = start_point, do_count = use_dataset )
	
	rows = res [ 'rows' ]
	tot_rows = res [ 'tot_rows' ]
	thumb_size = res [ 'thumb_size' ]

	if not use_dataset: 
		if not rows: rows = []
                return { 'items': rows, 
			'image_path': ga.cfg.get ( 'image.path', 'site/gallery/images' ), 
			'thumb_path': ga.cfg.get ( 'thumb.path', 'site/gallery/thumbs' ), 
			'thumb_size': thumb_size  }

	return _create_dataset ( start_point, quant, rows, tot_rows )

"""
no longer supported
def tags_list ( liwe ):
	ga = Gallery ( liwe )
	res = ga.invoke ( "tags.ajax.list_all_tags", module = "gallery" )
	if not res:
		res = []
	return res
"""

def get_templates ( liwe ):
	"""
	:INPUT:
	"""
        ga = Gallery ( liwe )
        return { "templates" : ga.get_templates () }


